<?php

namespace app\admin\model\jskmsg;

use app\admin\model\User;
use app\api\model\CategoryModel;
use app\api\model\GoodsModel;
use think\Model;
use traits\model\SoftDelete;

class Homeorder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'home_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'reserve_time_text',
        'status_text',
        'take_time_text',
        'complete_time_text',
        'cancel_time_text'
    ];
    

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5')];
    }


    public function getReserveTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['reserve_time']) ? $data['reserve_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTakeTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['take_time']) ? $data['take_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCompleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['complete_time']) ? $data['complete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCancelTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cancel_time']) ? $data['cancel_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setReserveTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTakeTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCompleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCancelTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    //关联用户表
    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id',[],'left')->setEagerlyType(0);
    }
    //关联用户表（接单员id）
    public function role()
    {
        return $this->belongsTo(User::class,'role_id','id',[],'left')->setEagerlyType(0);
    }
    //关联用户表（取消用户id）
    public function cancel()
    {
        return $this->belongsTo(User::class,'cancel_id','id',[],'left')->setEagerlyType(0);
    }

    //关联分类表
    public function category()
    {
        return $this->belongsTo(CategoryModel::class,'category_id','id',[],'left')->setEagerlyType(0);
    }

    //关联家政服务表
    public function goods()
    {
        return $this->belongsTo(GoodsModel::class,'goods_id','id',[],'left')->setEagerlyType(0);
    }


}
